<?php
/**
 * Widget to display a single Page Content
 * @package widgets.singlecontent
 * @author Rui Godinho
 */ 
class SingleContentWidget extends Widget {
	static $title = '';
	static $cmsTitle = 'Single Content';
	static $description = 'Adds content from a child page to the widget sidebar.';
	
	static $db = array(
		'WidgetTitle' => 'Varchar(255)',
		'SinglePage'=>'Varchar(255)'
		);
	static $defaults = array(
		'SinglePage'=>'home'
		);
	
	function Title() {
		return $this->WidgetTitle ? $this->WidgetTitle : self::$title;
	}
 
	function getCMSFields() {
		// TODO: Usar TreeDropdownField
		return new FieldSet(
			new TextField('WidgetTitle', _t('TITLE','Title (optional)')),
			new TextField('SinglePage', _t('SINGLEPAGE', 'The URL segment of the page to display.'))
		);
	}
	
	/**
	 * Return a Single Content Page
	 * @return DataObject
	 */
	function SingleContent() {
		$page = DataObject::get_one('SiteTree', "URLSegment = '". Convert::raw2sql($this->SinglePage) . "'");
		if(!$page) {
			return false;
		}
		else {
			return $page;
		}	}
}
